Welcome to pandas!

3.5 条件判断处理1(amsk与where)

Mask()函数与where()函数结构相同,含义相反。Mask()函数是对条件成立做处理,where()

函数是对条件不成立做处理,这两个函数均可以对Series和DataFrame进行判断处理。

import pandas as pd

s=pd.Series([ 89,150,96,114,96,120 ])

r=s.mask(s>100, "优" )

print (r)

返回:

0 89
1
2 96
3
4 96
5

dtype: object


import pandas as pd

df=pd.read_excel( "测试素材.学生成绩.xlsx","成绩表" )

t=df.民族.mask(df.民族!= "汉","*" )

t1=df.民族.where(df.民族!= "汉","*" )

print (df)

print (t)

print (t1)

返回:

姓名 民族 总分
0 张三 560
1 李四 470
2 王五 500

0 *
1
2 *

Name: 民族, dtype: object


0
1 *
2

Name: 民族, dtype: object